//
//  XBHeadLineController.m
//  Home
//
//  Created by 陈明茂 on 16/3/29.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "XBHeadLineController.h"

#import "HMLoopView.h"
#import "XBMenuCell.h"
#import "XBMenuLayout.h"


@interface XBHeadLineController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic) HMLoopView *loopView;

@property (strong, nonatomic) UICollectionView *collectionView;

@property (strong, nonatomic) UICollectionViewFlowLayout *layout;

@end

@implementation XBHeadLineController

#pragma mark - view的生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建轮播器
    NSArray * imageURLStrs = @[@"http://image1.xunbaozl.com/FlnFNSGYAFsElAwkn1Hq9TCwAFQy?imageMogr2",@"http://image1.xunbaozl.com/FqEeLNXs4WtSeZDy2cajBHnyukLO?imageMogr2",@"http://image1.xunbaozl.com/FkhmGZesCflsLCmtJEkw-erDXw9L?imageMogr2"];
    HMLoopView *loopView = [[HMLoopView alloc] initWithURLStrs:imageURLStrs titles:@[@"xxxx",@"yyyyyy",@"zzzzzzz"]  didSelected:^(NSInteger index) {
        
    }];
    // 轮播器
    self.loopView = loopView;
    [self.view addSubview:loopView];
    
    // 注册cell
    [self.collectionView registerClass:[XBMenuCell class] forCellWithReuseIdentifier:@"menu"];
    
    [self.view addSubview:self.collectionView];
}

#pragma mark - 布局子控件的方法

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    CGFloat collectionH = 100;
    self.loopView.frame = self.view.bounds;
    self.loopView.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height - collectionH);
    
    // 设置collectionView的frame值
    CGFloat collectionX = 0;
    CGFloat collectionY = CGRectGetHeight(self.loopView.frame);
    CGFloat collectionW = self.view.frame.size.width;
    self.collectionView.frame = CGRectMake(collectionX, collectionY, collectionW, collectionH);
    
}

#pragma mark - collectionView的代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XBMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"menu" forIndexPath:indexPath];
    
    return cell;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"xx");
}

#pragma mark - 懒加载
- (UICollectionView *)collectionView{
    
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[XBMenuLayout alloc] init]];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

@end
